package net.homeftp.heimo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import net.homeftp.heimo.camera.CameraPreview;
import net.homeftp.heimo.camera.DrawView;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.graphics.Rect;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.Toast;

/*
 * http://divided-games.com/drawing-with-canvas-on-top-of-a-camera-preview/
 * 
 * 
 * */
public class CameraActivity extends Activity {
	private final String TAG = "CameraActivity";
	private Camera mCamera;
	private CameraPreview mPreview;

	DrawView dv;
	FrameLayout alParent;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		setContentView(R.layout.camera_view);

		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		// // Create an instance of Camera
		// mCamera = getCameraInstance();
		// getCameraConfig();
		//
		// // Create our Preview view and set it as the content of our activity.
		// mPreview = new CameraPreview(this, mCamera);
		// FrameLayout preview = (FrameLayout)
		// findViewById(R.id.camera_preview);
		// preview.addView(mPreview);
	}

	public void fnCamera(View v) {
		// mCamera.takePicture(null, null, mPicture);
		mCamera.stopPreview();
		mCamera.startPreview();
	}

	@SuppressLint("NewApi")
	private void getCameraConfig() {
		// set Camera parameters
		Camera.Parameters params = mCamera.getParameters();
		if (params.getMaxNumMeteringAreas() > 0) { // check that metering areas
													// are supported
			List<Camera.Area> meteringAreas = new ArrayList<Camera.Area>();

			Rect areaRect1 = new Rect(-100, -100, 100, 100); // specify an area
																// in center of
																// image
			meteringAreas.add(new Camera.Area(areaRect1, 600)); // set weight to
																// 60%
			Rect areaRect2 = new Rect(800, -1000, 1000, -800); // specify an
																// area in upper
																// right of
																// image
			meteringAreas.add(new Camera.Area(areaRect2, 400)); // set weight to
																// 40%
			params.setMeteringAreas(meteringAreas);
		} else {
			Log.v(TAG, "MeteringAreas Not supported");
		}

		mCamera.setParameters(params);
	}

	/** Check if this device has a camera */
	private boolean checkCameraHardware(Context context) {
		if (context.getPackageManager().hasSystemFeature(
				PackageManager.FEATURE_CAMERA)) {
			// this device has a camera
			return true;
		} else {
			// no camera on this device
			return false;
		}
	}

	/** A safe way to get an instance of the Camera object. */
	public static Camera getCameraInstance() {
		Camera c = null;
		try {
			c = Camera.open(); // attempt to get a Camera instance
		} catch (Exception e) {
			// Camera is not available (in use or does not exist)
		}
		return c; // returns null if camera is unavailable
	}

	public void fnLoad() {
		mCamera = getCameraInstance();
		if (mCamera != null) {
			alParent = new FrameLayout(this);
			alParent.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
					LayoutParams.FILL_PARENT));

			dv = new DrawView(this);
			mPreview = new CameraPreview(this, mCamera,dv);
			
			alParent.addView(mPreview);
			alParent.addView(dv);

			setContentView(alParent);
		} else {
			Toast toast = Toast.makeText(getApplicationContext(),
					"Unable to find camera. Closing.", Toast.LENGTH_SHORT);
			toast.show();
			finish();
		}
	}

	private PictureCallback mPicture = new PictureCallback() {

		@Override
		public void onPictureTaken(byte[] data, Camera camera) {
			Log.d(TAG, "onPictureTaken");
			// File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
			// if (pictureFile == null){
			// Log.d(TAG,
			// "Error creating media file, check storage permissions: " +
			// e.getMessage());
			// return;
			// }
			//
			// try {
			// FileOutputStream fos = new FileOutputStream(pictureFile);
			// fos.write(data);
			// fos.close();
			// } catch (FileNotFoundException e) {
			// Log.d(TAG, "File not found: " + e.getMessage());
			// } catch (IOException e) {
			// Log.d(TAG, "Error accessing file: " + e.getMessage());
			// }
		}
	};

	@Override
	protected void onPause() {
		super.onPause();
		releaseCamera();
	}

	@Override
	protected void onResume() {
		super.onResume();
		fnLoad();
	}

	private void releaseCamera() {
		if (mCamera != null) {
			mCamera.release();
			mCamera = null;
		}
	}
}